package cn.tedu.blockingqueue;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;

public class PriorityBlockingQueueDemo {
    public static void main(String[] args) {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue<>(7);
        queue.put("e");
        queue.put("g");
        queue.put("h");
        queue.put("z");
        queue.put("m");
        queue.put("v");
        queue.put("b");

//        try {
//            for (int i = 0; i < 7; ++i) {
//                System.out.println(queue.take());
//            }
//        } catch (InterruptedException interruptedException) {
//            interruptedException.printStackTrace();
//        }

        // 按照分数进行降序排序
        Comparator<Student> c = (o1, o2) -> o2.getScore() - o1.getScore();

        PriorityBlockingQueue<Student> queue2 = new PriorityBlockingQueue<>(5, c);
        queue2.put(new Student("曹阳", 50, 50));
        queue2.put(new Student("许宁", 28, 59));
        queue2.put(new Student("小朴朴", 35, 70));
        queue2.put(new Student("陈哲", 18, 62));
        queue2.put(new Student("肖老板", 40, 51));

        Student ss = new Student("aa", 30, 60);
        System.out.println(ss.score);

        try {
            for (int i = 0; i < 5; ++i) {
                System.out.println(queue2.take());
            }
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }
    }
}

class Student implements Comparable<Student> {
    String name;
    int age;
    int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    /**
     * 指定比较规则
     * 如果this.compareTo(o) > 0, 那么this就拍到o后面
     * 如果this.compareTo(o) < 0, 那么this就拍到o前面
     * 在底层进行升序排序
     * 按照年龄升序排序
     * 升序排序: this - o
     * 降序排序: o - this
     * @param o
     * @return
     */
    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
